package cn.yjr.vector;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import cn.yjr.fragment.FragmentFactory;
import cn.yjr.vector.tools.ApplicationData;
import cn.yjr.vector.tools.SharedPreferencesHelper;
import cn.yjr.vector.tools.YJRApplication;
import cn.yjr.vector.tools.YJRHttpClient;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.support.v4.app.Fragment;

/**
 * @author vector
 *
 */
public class TabHomeActivity extends SherlockFragmentActivity{
	private FragmentManager fragmentManager;
	private RadioGroup radioGroup;
	private RadioButton grzx_rb;
	Context context;
	boolean isExit;
	FragmentTransaction transaction;
	JSONObject initData;
	JSONObject userData;
	String version;
	SharedPreferencesHelper spHelper;
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options;
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_layout);
        ActionBar actionBar = getSupportActionBar();
        context = this;
        isExit = false;
        actionBar.hide();
        spHelper = new SharedPreferencesHelper(context);
        version = spHelper.getStringValue("version", "0");
        try {
			getInitData();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        fragmentManager = getSupportFragmentManager();
        radioGroup = (RadioGroup) findViewById(R.id.rg_tab);
        grzx_rb = (RadioButton) findViewById(R.id.grzx);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
            	int index = 1;
            	switch(checkedId){
            	case R.id.sy:
            		index  =1;
            		break;
            	case R.id.zxs:
            		index = 2;
            		break;
            	case R.id.zcxm:
            		index = 3;
            		break;
            	case R.id.grzx:
            		index = 4;
            		break;
            	}
                Fragment fragment = FragmentFactory.getInstanceByIndex(index,imageLoader);
                if(fragment==null){
                	Log.v("weiyi","is null");
                }else{
                	transaction = fragmentManager.beginTransaction();
                	transaction.replace(R.id.frame_content, fragment);
                    transaction.commit();
                }
            }
        });
        grzx_rb.setChecked(true);
    }
	
	/**
	 * @throws JSONException 
	 * 
	 */
	private void getInitData() throws JSONException {
		initData = YJRApplication.initData;
		version = initData.getString("dataVersion");
		spHelper.putString("version", version);
		spHelper.putString("initData", initData.toString());
		
	}

	@Override
	  public boolean onKeyDown(int keyCode, KeyEvent event)
	  {
	    if (keyCode == KeyEvent.KEYCODE_BACK)
	      if (!this.isExit)
	      {
	        this.isExit = true;
	        Toast.makeText(this.context, "再点击一次退出", 0).show();
	        new Handler().postDelayed(new Runnable()
	        {
	          public void run()
	          {
	            TabHomeActivity.this.isExit = false;
	          }
	        }
	        , 2000L);
	      }else{
	    	  finish();
	      }
	    return super.onKeyUp(keyCode, event); 
	  }
}
